package com.base.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.SelfShopWithdrawals;
import com.base.po.voip.Users;
import com.base.service.SelfShopOrderService;
import com.base.service.SelfShopWithdrawalsService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * 自营商城提现
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopWithdrawals {
    @Autowired
    private SelfShopWithdrawalsService selfShopWithdrawalsService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private SelfShopOrderService selfShopOrderService;

    // 自营商城订单首页
    @RequestMapping("/selfShopWithdrawals")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        //提现记录
        List<SelfShopWithdrawals> selfShopWithdrawals = null;
        page.setCurrentPageNo(1);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(selfShopWithdrawalsService.countByAll(adminUserId, null, null, null, null, null));
            selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, adminUserId, null, null, null, null, null);
        }
        if (users.getRole() == 2) {
            page.setRecordTotal(selfShopWithdrawalsService.countByAll(users.getParentid(), adminUserId, null, null, null, null));
            selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, users.getParentid(), adminUserId, null, null, null, null);
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("status", 4);
        request.setAttribute("selfShopWithdrawals", selfShopWithdrawals);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopWithdrawals");
        return modelAndView;
    }

    @RequestMapping("/selectSelfWithdrawals")
    public ModelAndView selectSelfWithdrawals(HttpServletRequest request, Integer curPage, String shopname,
                                              String name, String accountnum, Byte status) {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        //已有商品
        List<SelfShopWithdrawals> selfShopWithdrawals = null;
        Integer choose = 2;
        if (shopname != null && !shopname.equals("") || name != null && !name.equals("")
                || accountnum != null && !accountnum.equals("") || status != 4) {
            choose = 1;
        }

        String fShopName = null;
        String fName = null;
        String fAccountNum = null;
        Byte fStatus = null;

        if (shopname != null && !shopname.equals("")) {
            fShopName = shopname;
        }
        if (name != null && !name.equals("")) {
            fName = name;
        }
        if (accountnum != null && !accountnum.equals("")) {
            fAccountNum = accountnum;
        }
        if (status != 4) {
            fStatus = status;
        }

        page.setCurrentPageNo(curPage);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(selfShopWithdrawalsService.countByAll(adminUserId, null, fShopName, fName, fAccountNum, fStatus));
            selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, adminUserId, null, fShopName, fName, fAccountNum, fStatus);
        }
        if (users.getRole() == 2) {
            page.setRecordTotal(selfShopWithdrawalsService.countByAll(users.getParentid(), adminUserId, fShopName, fName, fAccountNum, fStatus));
            selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, users.getParentid(), adminUserId, fShopName, fName, fAccountNum, fStatus);
        }

        if (choose == 2) {
            if (users.getRole() == 0 || users.getRole() == 1) {
                page.setRecordTotal(selfShopWithdrawalsService.countByAll(adminUserId, null, null, null, null, null));
                selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, adminUserId, null, null, null, null, null);
            }
            if (users.getRole() == 2) {
                page.setRecordTotal(selfShopWithdrawalsService.countByAll(users.getParentid(), adminUserId, null, null, null, null));
                selfShopWithdrawals = selfShopWithdrawalsService.selectByAll(page, users.getParentid(), adminUserId, null, null, null, null);
            }
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("shopname", shopname);
        request.setAttribute("name", name);
        request.setAttribute("status", status);
        request.setAttribute("accountnum", accountnum);
        request.setAttribute("selfShopWithdrawals", selfShopWithdrawals);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopWithdrawals");
        return modelAndView;
    }

    // 提现
    @RequestMapping("/addSelfShopWithdrawals")
    public ModelAndView addSelfShopWithdrawals(HttpServletRequest request) {
        BigDecimal money = new BigDecimal(0); // 可提现金额
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade"); // 类型
        String adminUserId = (String) request.getSession().getAttribute("adminUserId"); // id
        if (adminUserId != null && !adminUserId.isEmpty() && adminUsergrade == 2) {
            Users users = usersService.selectByPrimaryKey(adminUserId); // 商家信息
            if (users != null && users.getDatastatus() == true) {
                Users users2 = usersService.selectByPrimaryKey(users.getParentid()); // 代理商
                BigDecimal ratio = new BigDecimal(1); // 比例1
                if (users2 != null && users2.getDatastatus() == true) {
                    ratio = new BigDecimal(users2.getShopratio()); // 代理商比例
                }
                System.out.println(selfShopOrderService.sumAllCash(users.getParentid(), users.getId()));
                System.out.println(ratio);
                System.out.println(selfShopWithdrawalsService.sumAllCash(users.getParentid(), users.getId()));
                money = selfShopOrderService.sumAllCash(users.getParentid(), users.getId()).multiply(ratio)
                        .subtract(selfShopWithdrawalsService.sumAllCash(users.getParentid(), users.getId())); // 商家所有收益*比例-提现的收益
            }
        }
        return new ModelAndView("jsp/add_SelfShopWithdrawals2", "money", money.intValue());
    }
}
